package net.blogways.ordermgr.user;

import com.asiainfo.bpc.service.SqlCodeService;
import com.asiainfo.bpc.utils.ToolFactory;
import com.asiainfo.utils.Utils;
import net.blogways.ordermgr.util.ErrorCode;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: gaoxiaobo
 * @Date: 2018/7/25 16:15
 * @Description:
 */
@Component("user:userMgr")
public class UserManager implements Command {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean execute(Context context) throws Exception {
        return false;
    }

    /**
     * 删除用户
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean deleteUsers(Context context)throws Exception{
        List users = (List) context.get("users");
        Map sqlParams = new HashMap();

        List userNames = new ArrayList();
        for (int i = 0; i < users.size(); i++) {
            Map user = (Map) users.get(i);
            userNames.add(user.get("USER_NAME"));
        }

        sqlParams.put("USERS", userNames);

        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();

        sqlCodeService.update("DEL_ROLE_BY_USER", "TD_M_USER_ROLE", sqlParams);//删除用户角色
        sqlCodeService.update("UPD_USER_DEL", "TD_M_USER", sqlParams);
        return Utils.returnOkResult(context, "员工删除成功！");
    }

    /**
     * 新建员工
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean createUser(Context context)throws Exception{
        Map user = (Map) context.get("user");

        String userType = user.get("USER_TYPE").toString();
        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        Map sqlParams = new HashMap();
        sqlParams.put("VDICT_CODE", userType);
        sqlParams.put("VDICT_TYPE_CODE", "userType");
        sqlParams.put("VLOCALE", "zh");
        List roles = sqlCodeService.select("SEL_ROLE_BY_USER_TYPE", "TD_S_DATA_DICT", sqlParams);
        if (roles.size() != 1){
            return Utils.returnFalseResult(context, ErrorCode.DATA_ERROR, "角色配置有误！");
        }

        List sameName = sqlCodeService.select("SEL_USER_BY_NAME", "TD_M_USER", user);
        if (sameName.size() > 0){
            return Utils.returnFalseResult(context, ErrorCode.USER_NAME_EXISTS, "用户名已存在！");
        }

        Map role = (Map) roles.get(0);
        String roleCode = role.get("REMARK").toString();

        sqlCodeService.insert("INT_COMM", "TD_M_USER", user);

        user.put("ROLE_CODE", roleCode);
        sqlCodeService.insert("INT_USER_ROLE", "TD_M_USER_ROLE", user);

        return Utils.returnOkResult(context, "员工新建成功！");
    }

    /**
     * 编辑员工
     * @param context
     * @return
     * @throws Exception
     */
    public Boolean editUser(Context context)throws Exception{
        Map user = (Map) context.get("user");
        String oldUserName = context.get("OLD_USER_NAME").toString();

        SqlCodeService sqlCodeService = ToolFactory.getInstance().getSqlCodeService();
        Map sqlParams = new HashMap();
        sqlParams.put("USER_NAME", oldUserName);
        List oldUsers = sqlCodeService.select("SEL_USER_BY_NAME", "TD_M_USER", sqlParams);
        if (oldUsers.size() != 1){
            return Utils.returnFalseResult(context, ErrorCode.DATA_ERROR, "该用户不存在！");
        }

        Map oldUser = (Map) oldUsers.get(0);

        if (!oldUserName.equals(user.get("USER_NAME"))){
            List sameNames = sqlCodeService.select("SEL_USER_BY_NAME", "TD_M_USER", user);
            if (sameNames.size() > 0){
                return Utils.returnFalseResult(context, ErrorCode.USER_NAME_EXISTS, "用户名已存在！");
            }
        }

        sqlParams.clear();
        sqlParams.put("USERS", oldUser.get("USER_NAME"));
        sqlCodeService.update("DEL_ROLE_BY_USER", "TD_M_USER_ROLE", sqlParams);

        sqlParams.put("VDICT_CODE", user.get("USER_TYPE"));
        sqlParams.put("VDICT_TYPE_CODE", "userType");
        sqlParams.put("VLOCALE", "zh");
        List roles = sqlCodeService.select("SEL_ROLE_BY_USER_TYPE", "TD_S_DATA_DICT", sqlParams);
        if (roles.size() != 1){
            return Utils.returnFalseResult(context, ErrorCode.DATA_ERROR, "角色配置有误！");
        }

        Map role = (Map) roles.get(0);
        String roleCode = role.get("REMARK").toString();
        user.put("ROLE_CODE", roleCode);
        sqlCodeService.insert("INT_USER_ROLE", "TD_M_USER_ROLE", user);

        user.put("OLD_USER_NAME", oldUserName);
        sqlCodeService.update("UPD_USER_INFO", "TD_M_USER", user);
        return Utils.returnOkResult(context, "员工编辑成功！");
    }
}
